import jieba
from sklearn.manifold import TSNE
from gensim.models import Word2Vec
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
#r"C:\Users\User\Desktop\code\pycode\weibo.txt"
#r"C:\Users\User\Desktop\python5-pre-trained-weibo-word2vec\weibo_59g_embedding_200.model"
class TextAnalyzer:
    def __init__(self,txt_path=None,model_path=None,corpus_file=None,
                 vector_size=None,window=None,min_count=None):
        self.txt_path = txt_path
        self.model_path = model_path
        self.corpus_file = corpus_file
        self.vector_size = vector_size
        self.window = window
        self.min_count = min_count
    def text(self):
        stopset=[]
        with open(r'C:\Users\User\Desktop\code\pycode\baidu_stopwords.txt',encoding="utf-8") as fs:
            for line1 in fs.readlines():
                stopset.append(line1.strip('\n'))
        sentences = []
        with open(self.txt_path,'r',encoding="utf-8") as f:
            for line in f:
                sen = [w for w in jieba.cut(line.strip().split('\t')[1]) if w not in stopset]
                #print(sen)
                sentences.append(sen)
        return sentences    
    def build_model(self):
        return Word2Vec(sentences=self.text(), vector_size=self.vector_size, 
                        window=self.window, min_count=self.min_count)
    
    def get_most_similar(self,word,num):
        return self.build_model().wv.most_similar(word, topn=num)
    
    def get_least_similar(self,word,num):
        return self.build_model().wv.most_similar(negative=[word], topn=num)
    
    def save_model(self):
        self.build_model().save(self.model_path)
    
    def load_model(modelx_path):
        return Word2Vec.load(modelx_path)
    
txtpath = r"C:\Users\User\Desktop\code\pycode\weibo.txt"
modelpath = r"C:\Users\User\Desktop\code\pycode\lab5\model1.model"
vectorsize = 200
windowx = 5
mincount = 1
wordx = "快乐"
num = 5

M = TextAnalyzer(txt_path=txtpath,model_path=modelpath,vector_size=vectorsize,
                 window=windowx,min_count=mincount)
M.save_model()
print(M.get_most_similar(wordx,num))
    
modelx =TextAnalyzer.load_model(r"C:\Users\User\Desktop\python5-pre-trained-weibo-word2vec\weibo_59g_embedding_200.model")
print(modelx.wv.most_similar(wordx, topn=10))
#print(modelx.wv.most_similar(negative=[wordx], topn=10))
        